package ishitong.vehicleManagement.service.biz.impl;

import ishitong.commons.pagination.Pagination;
import ishitong.commons.pagination.PaginationResult;
import ishitong.vehicle.common.model.Drivingstrategy;
import ishitong.vehicle.common.model.Lane;
import ishitong.vehicle.common.model.LaneConfig;
import ishitong.vehicle.common.model.Monitor;
import ishitong.vehicle.common.model.MonitorAndDirection;
import ishitong.vehicle.common.service.biz.DrivingstrategySerivce;
import ishitong.vehicle.common.service.biz.LaneConfigService;
import ishitong.vehicle.common.service.biz.LaneService;
import ishitong.vehicle.common.service.biz.MonitorAndDirectionService;
import ishitong.vehicle.common.service.biz.MonitorService;
import ishitong.vehicleManagement.model.MonitoringPoint;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ishitong.platform.common.page.Page;

/** @pdOid 78994f22-cb42-401d-bcca-78339262e8c6 */
@Service(value = "spiMonitoringPointService")
@Transactional
public class SPIMonitoringPointService {
   /** @pdOid 185f4a77-7d0c-4449-b27c-eded2a606c0b */
   @Autowired
   private MonitorService monitorService;
   /** @pdOid 578d4906-c67b-4d3b-906e-e4ad4d546cb1 */
   @Autowired
   private MonitorAndDirectionService monitorAndDirectionService;
   /** @pdOid 7ce8f370-5d95-4639-9aa8-f87999123656 */
   @Autowired
   private LaneService laneService;
   @Autowired
   private LaneConfigService laneConfigService;
   @Autowired
   private DrivingstrategySerivce drivingstrategySerivce;
   
   /** @param monitorAndDirection
    * @pdOid 6f7131ad-ce67-4691-9e23-b730003984c3 */
   private MonitoringPoint setMonitoringPoint(MonitorAndDirection monitorAndDirection) {
    MonitoringPoint monitoringPoint=new MonitoringPoint();
	BeanUtils.copyProperties(monitorAndDirection.getMonitor(), monitoringPoint);
	monitoringPoint.setSection(monitorAndDirection.getSection());
	return monitoringPoint;
   }
   
   /** @param monitoringPoint
    * @pdOid 8bfdf86c-3f7c-44f8-9121-dea3b6d8daf8 */
   private MonitorAndDirection setMonitorAndDirection(MonitoringPoint monitoringPoint) {
	MonitorAndDirection  monitorAndDirection = new  MonitorAndDirection();
	Monitor monitor = new Monitor();
	BeanUtils.copyProperties(monitoringPoint, monitor);
	monitorAndDirection.setMonitor(monitor);
	monitorAndDirection.setSection(monitoringPoint.getSection());
	return monitorAndDirection;
   }
   
   /** @param monitoringPoint 
    * @param pagination
    * @pdOid 13f8ae7b-2eb7-4b86-899c-7245f26ca477 */
   public Page<MonitoringPoint> findPage(MonitoringPoint monitoringPoint, Pagination pagination) {
   	MonitorAndDirection  monitorAndDirection = new  MonitorAndDirection();
   	monitorAndDirection=setMonitorAndDirection(monitoringPoint);
   	PaginationResult<MonitorAndDirection> pr = monitorAndDirectionService.queryForPage(monitorAndDirection, pagination);
   	pagination = pr.getPagination();
   	List<MonitoringPoint> listMonitoringPoint = new ArrayList<MonitoringPoint>();
   	for (MonitorAndDirection mad: pr.getResults()) {
   		listMonitoringPoint.add(setMonitoringPoint(mad));
   	}
   	Page<MonitoringPoint> page = new Page<MonitoringPoint>(pagination.getCurrentPage(),pagination.getPageSize(),pagination.getTotalRows(),listMonitoringPoint);
   	return page;
   }
   
   /** @param ids
    * @pdOid 1d786b30-821e-435c-970d-7fb65704e8d4 */
   public void deleteMonitoringPointByIds(String ids) {
   	monitorService.deleteMonitorById(Long.valueOf(ids));
   }
   
   /** @param monitoringPointId
    * @pdOid 4b9ed1f0-1780-4ec7-8632-bf0a8927de79 */
   public List<Lane> findMonitoringLaneByMonitoringPointId(String monitoringPointId) {
	Lane lane = new Lane();
	lane.setMonitorid(Long.valueOf(monitoringPointId));
   	List<Lane> listLane = laneService.getLaneByCondition(lane);
   	return listLane;
   }
   
   /** @param monitoringPoint
    * @pdOid b36e8825-146b-4048-93c1-eaf079c47861 */
   public void saveMonitoryPoint(MonitoringPoint monitoringPoint) {
   	Monitor monitor = new Monitor();
   	BeanUtils.copyProperties(monitoringPoint, monitor);
   	if(monitoringPoint.getId() != null){
   		monitorService.modifyMonitor(monitor);
   	} else {
   		monitorService.addMonitor(monitor);
   	}
   }

	public void deleteMonitoringPointLaneById(String monitoringLaneId) {
		laneService.deleteLaneById(Long.valueOf(monitoringLaneId));
	}
	
	public MonitoringPoint findMonitoringPointById(String monitoringPointId){
		MonitoringPoint monitoringPoint=new MonitoringPoint();
		monitoringPoint.setId(Long.valueOf(monitoringPointId));
		MonitorAndDirection  monitorAndDirection = new  MonitorAndDirection();
	   	monitorAndDirection=setMonitorAndDirection(monitoringPoint);
	    List<MonitorAndDirection>	lstMonitorAndDirection=monitorAndDirectionService.queryMonitorAndDirection(monitorAndDirection);
	    if (lstMonitorAndDirection.size() > 0) {
	    	monitorAndDirection = lstMonitorAndDirection.get(0);
	    	monitoringPoint=setMonitoringPoint(monitorAndDirection);
	        return monitoringPoint;
	       }
	    return null;
	}
	
	public Lane findMonitoringLaneById(String monitoringLaneId) {
		Lane lane = new Lane();
		lane.setId(Long.valueOf(monitoringLaneId));
	   	List<Lane> lstLane = laneService.getLaneByCondition(lane);
	   	if (lstLane.size()>0) {
			lane=lstLane.get(0);
		   	List<LaneConfig> listLaneConfig = laneConfigService.getLaneConfigByLaneId(lane.getId());
		   	List<Drivingstrategy> lstDrivingstrategy= drivingstrategySerivce.getDrivingstrategyByLaneId(lane.getId());
		   	lane.setDrivingstrategyList(lstDrivingstrategy);
		   	lane.setLaneConfigList(listLaneConfig);
	   	}
		return lane;
	}

	public void saveMonitoringLane(Lane lane) {
		if(lane.getId() != null){
			laneService.modifyLane(lane);
	   	} else {
	   		laneService.addLane(lane);
	   	}
	}
}